//****************************************************************************
// Copyright (c) 2011-2013,WebJet Business Division,CYOU

// http://www.genesis-3d.com.cn

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//*****************************************************************************
Shader "PostEffect_SunShaft2" {
	Parameters{	
		Vector [texToTexParams] = "0.00066713482, 0.00036231885, -0.00017556180, 0.0013913043"

		Vector [SunShafts_SunPos] = "30.000000,60.387765,0,1"
		Vector [SunShafts_SunCol]   = "0.8,0.8,0.6,1"

		Vector [PI_sunShaftsParam1] = "0.1,0.76319116,0.0,0.0"
		Vector [PI_sunShaftsParam4] = "0.0050584702,1.1844447,0.0,0.0"
		
		sampler2D [p4RT] = "white"
	}
	
	RenderQueue "CUSTOMDRAW"
	

			CODEBEGIN
			
			float4		SunShafts_SunPos;
			float4      PI_sunShaftsParam4;
			float4      SunShafts_SunCol;
			
			sampler2D p4RT;
			struct VS_OUTPUT
			{
				float4 Position		: POSITION0;   // vertex position 
				float4 PosTex		: TEXCOORD0;//xy is positon, and zw is texCoords coord
			};
			struct PS_INPUT
			{
				float4 PosTex		: TEXCOORD0;
			};
			
			VS_OUTPUT mainVs(in float3 vPos : POSITION)
			{
				VS_OUTPUT Output;
				//float2 pos = vPos.xy * gTransform.zw + gTransform.xy;//
								
				Output.Position.xy = vPos.xy;	
				Output.Position.zw = float2(0.9f, 1.0f);
				Output.PosTex.xy = vPos.xy;//pos
				Output.PosTex.zw = 0;
				return Output;
			}


			float3 blendSoftLight(float3 a, float3 b)
			{
			  float3 c = 2 * a * b * ( 1 + a * (  1 - b ) ); // 4 inst
			 
			  float3 a_sqrt = sqrt( a );  // 1
			  float3 d = ( a  +  b * (a_sqrt - a )) * 2 - a_sqrt; // 3 inst
				
				if ( b.x < 0.5 && b.y < 0.5 && b.z <0.5 )
					d = c;
				return d;
			
			}

			float4 mainPs(PS_INPUT o) : COLOR0
			{
				float2 uv = getUVfromscreenspace(o.PosTex.xy);
				float4 gl_FragColor;
				float4 cScreen = tex2D(g_MainBuffer, uv);    
			
				float3 GRAY	=	float3(0.456,0.5,0.1);	
				float OScale	=	2.0;
				float2 magic = float2(0.000351,0.000725);
				float2 Offset	=	1*magic;
				float4 G0	=	float4(	dot(GRAY,float3(tex2D(g_MainBuffer, uv+Offset*float2(-OScale,-OScale)).xyz)),
										dot(GRAY,float3(tex2D(g_MainBuffer, uv+Offset*float2( OScale,-OScale)).xyz)),
										dot(GRAY,float3(tex2D(g_MainBuffer, uv+Offset*float2(-OScale, OScale)).xyz)),
										dot(GRAY,float3(tex2D(g_MainBuffer, uv+Offset*float2( OScale, OScale)).xyz)));

				float4 G1	=	float4(	dot(GRAY,float3(tex2D(g_MainBuffer, uv+Offset*float2(	 0.0,-OScale)).xyz)),
										dot(GRAY,float3(tex2D(g_MainBuffer, uv+Offset*float2(-OScale,    0.0)).xyz)),
										dot(GRAY,float3(tex2D(g_MainBuffer, uv+Offset*float2( OScale,    0.0)).xyz)),
										dot(GRAY,float3(tex2D(g_MainBuffer, uv+Offset*float2(    0.0, OScale)).xyz)));
				
				float2 Tan	=	float2(	dot(G0,float4(1.0,1.0,-1.0,-1.0))+G1.x-G1.w,
										dot(G0,float4(-1.0,1.0,-1.0,1.0))+G1.z-G1.y)*Offset;

				float4 C0	=	tex2D(g_MainBuffer, uv+Tan*1.4*2);
				float4 C1	=	tex2D(g_MainBuffer, uv+Tan*0.7*2);
				float4 C2	=	tex2D(g_MainBuffer, uv-Tan*0.7*2);
				float4 C3	=	tex2D(g_MainBuffer, uv-Tan*1.4*2);


				cScreen 	=	(cScreen+C0+C1+C2+C3)*0.2;
				//gl_FragColor 	= 	cScreen;			
				
				float fShaftsMask = 0;
				float4 cSunShafts = tex2D(p4RT, uv);
				fShaftsMask = clamp ( 1.00001- cSunShafts.w, 0.0, 1.0 )* 2.0 * PI_sunShaftsParam4.x;
				
				vec3 sunColor = SunShafts_SunCol.xyz;
				gl_FragColor.xyz = cScreen + cSunShafts.xyz * PI_sunShaftsParam4.y * sunColor * ( 1 - cScreen );
				
				gl_FragColor.xyz = blendSoftLight(gl_FragColor, sunColor * fShaftsMask *0.5+0.5);
	
				gl_FragColor.w = 1;	
				return gl_FragColor;	
			}
			CODEEND
	Technique "PostEffect_SunShaft2"{
		Pass { 
		Compiler{
		        VertexShader = mainVs
				PixelShader = mainPs
		         }
		        RenderSTATE { 
						cullMode "Back"
						ZTest Off
						ZWrite Off
						blendMode Off
						alphatest Off
						samplerState "g_MainBuffer" [Clamp] [Point]
				     }


		}
	}

}



                                                                                                               